package com.huichenghe.xinlvsh01.Ble;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.huichenghe.xinlvsh01.DbEntities.MyDBHelperForDayData;
import com.huichenghe.xinlvsh01.Utils.FormatUtils;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;

/**
 * 获取和处理以及响应手环数据
 * Created by lixiaoning on 15-12-7.
 */
public class BleDataForSleepData extends BleBaseDataManage
{
    public static final String TAG = BleDataForSleepData.class.getSimpleName();
    private Context mContext;

    public static final byte toDevice = (byte)0x26;
    public static final byte fromDevice = (byte)0xa6;
    int date, month, year;



    public BleDataForSleepData(Context mContext)
    {
        this.mContext = mContext;
        Calendar calendar = Calendar.getInstance();
        date = calendar.get(Calendar.DATE);
        month = calendar.get(Calendar.MONTH) + 1;
        year = calendar.get(Calendar.YEAR) - 2000;


    }


    /**
     * 获取睡眠数据
     */
    public void getTodaySleepdata()
    {
        byte[] bytes = new byte[4];
        bytes[0] = (byte)(date & 0xff);
        bytes[1] = (byte)(month & 0xff);
        bytes[2] = (byte)(year & 0xff);
        bytes[3] = (byte)0x02;

        setMsgToByteDataAndSendToDevice(toDevice, bytes, bytes.length, true);
    }


    /**
     * 处理手环返回的数据
     * @param bufferTmp
     */
    public void dealTheSleepData(byte[] bufferTmp)
    {
//        070c0f02ffffffffffffffffffffffffffffffffff3f000000030000c0ffffffffffffffffffffff4016
        Log.i(TAG, "睡眠数据：" + FormatUtils.bytesToHexString(bufferTmp));
        int d = bufferTmp[0] & 0xff;
        int m = bufferTmp[1] & 0xff;
        int y = bufferTmp[2] & 0xff;
        /**
         * 每2位表示十分钟的睡眠质量， 0：活动，1：浅睡，2：深睡，3：未检测
         * 模拟的睡眠数据
         * ff ff ff ff ff ff ff ff ff ff
         * ff ff ff ff ff ff ff 3f 00 00
         * 00 03 00 00 c0 ff ff ff ff ff
         * ff ff ff ff ff ff 4016
         */
        int[] dataS = new int[36 * 4];
        int index = 0;
        for (int i = 0; i < 36; i++)        // 遍历字节数组
        {
            byte buffer = bufferTmp[i];     // 取出一个字节赋值给buffer

            for (int j = 0; j < 4; j++)    // 循环取出两位
            {
                int a = (buffer >> (3 - j) * 2) & (byte) 0x03;   // 取出两位
                dataS[index] = a;
                index++;
            }

        }

        String dataString = "";
        for (int i = 0; i < dataS.length; i ++)
        {
            dataString += String.valueOf(dataS[i]);
        }


        String userAccount = LocalDataSaveTool.getInstance(mContext).readSp(MyConfingInfo.USER_ACCOUNT);

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DATE);
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR) - 2000;
        String dateSleep = (year + 2000) + "-" + month + "-" + day;

        // 先查询数据库有无睡眠数据
        Cursor mCursor = MyDBHelperForDayData.getInstance(mContext).selectTheSleepData(mContext, userAccount, dateSleep);
        if(mCursor.getCount() != 0) // 等于0代表无数据
        {
            MyDBHelperForDayData.getInstance(mContext).updateTheSleepData(mContext, userAccount, dateSleep,dataString);
        }
        else
        {
            MyDBHelperForDayData.getInstance(mContext).insertTheSleepData(mContext, userAccount, dateSleep, dataString);
        }







        // 判断是否是当天数据，不是则返回响应数据
        if(d != date || m != month || y != year)
        {
            Log.i(TAG, "时间对比：" + d + "--" + date + "--" + m + "--" + month + "--" + y + "--" + year);
            byte[] backData = new byte[4];
            for (int i = 0; i < backData.length; i++)
            {
                backData[i] = bufferTmp[i];
            }
            setMsgToByteDataAndSendToDevice(toDevice, backData, backData.length, true);
        }






    }
}
